//
//  MonitorViewController.m
//  SIPphone
//
//  Created by wrt on 14-3-21.
//  Copyright (c) 2014年 wrtsoft. All rights reserved.
//

#import "MonitorViewController.h"
#import "AddMonitorViewController.h"
#import "CallViewController.h"

@interface MonitorViewController ()

@end

@implementation MonitorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"Monitor", @"监视 Monitor");
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.monitorArray = [[NSMutableArray alloc] init];
    self.monitorArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNAME"];
    self.numberArray = [[NSMutableArray alloc] init];
    self.numberArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNUMBER"];
    [self.tableView reloadData];
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    interface.delegate = self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.monitorArray = [[NSArray alloc] initWithObjects:@"小门口机",@"分机", nil];
//    self.imageArray = [[NSArray alloc] initWithObjects:@"小门口机.png",@"分机.png", nil];
//    self.numberArray = [[NSArray alloc] initWithObjects:@"8888",@"8889", nil];
    UIBarButtonItem *addMonitorButton = [[UIBarButtonItem alloc] initWithTitle:@"十" style:UIBarButtonItemStylePlain target:self action:@selector(pushAddmonitor)];
    self.navigationItem.rightBarButtonItem = addMonitorButton;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.monitorArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNAME"];
    self.numberArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNUMBER"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushAddmonitor
{
    AddMonitorViewController *addMonitorView = [[AddMonitorViewController alloc] init];
    [self.navigationController pushViewController:addMonitorView animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.monitorArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"monitorCellIdentifer";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    cell.textLabel.text = self.monitorArray[indexPath.row];
    cell.detailTextLabel.text = self.numberArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"监视.png"];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //先判断是否是登陆状态，如果不是登陆状态则提示请登陆
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL registerStatus = [defaults boolForKey:@"REGISTERSTATUS"];
    if (registerStatus && [(NSString *)self.numberArray[indexPath.row] length] > 1)
    {
        self.tempNumber = [[NSString alloc] init];
        self.tempNumber = self.numberArray[indexPath.row];
        SIPInterface *interface = [SIPInterface shareSIPInterface];
        [interface start_Call:self.numberArray[indexPath.row]];
        
    }
    else if(!registerStatus)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"please log in", @"请先登陆 Please log in") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
        [alert show];
    }
}

//编辑模式下可删除的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.numberArray removeObjectAtIndex:indexPath.row];
    [self.monitorArray removeObjectAtIndex:indexPath.row];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:self.monitorArray forKey:@"SAVEMONITORNAME"];
    [defaults setValue:self.numberArray forKey:@"SAVEMONITORNUMBER"];
    [defaults synchronize];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}


#pragma mark -
#pragma mark SIPlistenMethod
- (void)outgoingCallBegin  //主动呼叫开始
{
    dispatch_async(dispatch_get_main_queue(),^{
        CallViewController *callVC = [[CallViewController alloc] initWithNumber:self.tempNumber];
        callVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:callVC animated:YES];
    });
}
- (void)registerSuccessful //注册成功时的操作
{
    dispatch_async(dispatch_get_main_queue(),^{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:@"REGISTERSTATUS"];
        [defaults synchronize];
    });
}

- (void)registerFailed  //注册失败时的操作
{
    dispatch_async(dispatch_get_main_queue(),^{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:NO forKey:@"REGISTERSTATUS"];
        [defaults synchronize];
    });
}

- (void)callEnding
{
    
}

- (void)openWindow
{
    
}


@end
